package com.mooc.yohoshop.ui.car;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.mooc.yohoshop.App;
import com.mooc.yohoshop.R;
import com.mooc.yohoshop.annotation.CheckLogin;
import com.mooc.yohoshop.dao.DaoDBUtils;
import com.mooc.yohoshop.dao.OffLineCarDaoEntity;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.CarEntity;
import com.mooc.yohoshop.entity.CreateOrderEntity;
import com.mooc.yohoshop.entity.MergeSuccessEntity;
import com.mooc.yohoshop.entity.OffLineCarEntity;
import com.mooc.yohoshop.entity.SuccessEntity;
import com.mooc.yohoshop.ui.base.BaseActivity;
import com.mooc.yohoshop.ui.checkorder.CheckOrderActivity;
import com.mooc.yohoshop.widget.SecKillDivider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CarActivity extends BaseActivity<CarRepo> implements View.OnClickListener,
        OnItemChildClickListener {

    private TextView car_all_tv,carlist_edt_tv;
    private Button carlist_order_btn;
    private ImageView car_sel_all;
    private RecyclerView carlist_rv;
    private CarAdapter adapter;
    private CarEntity carEntity;
    private List<HashMap<String,Object>>list = new ArrayList<>();
    private int num = 0;
    private float totalPrice = 0f;
    private boolean allFlag = true;

    @Override
    protected int layout() {
        return R.layout.activity_carlist;
    }

    @Override
    protected void init() {
        f(R.id.carlist_back_icon).setOnClickListener(this);
        carlist_order_btn = f(R.id.carlist_order_btn);
        carlist_order_btn.setOnClickListener(this);
        f(R.id.car_sel_all).setOnClickListener(this);
        f(R.id.carlist_edt_tv).setOnClickListener(this);
        car_sel_all = f(R.id.car_sel_all);
        carlist_rv = f(R.id.carlist_rv);
        car_all_tv = f(R.id.car_all_tv);
        carlist_edt_tv = f(R.id.carlist_edt_tv);
        carlist_edt_tv.setOnClickListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        carlist_rv.setLayoutManager(manager);
        carlist_rv.addItemDecoration(new SecKillDivider(this));
        adapter = new CarAdapter();
        adapter.setOnItemChildClickListener(this);
        carlist_rv.setAdapter(adapter);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        //获取离线购物车或者在线购物车列表
        HashMap<String,Object>map = new HashMap<>();
        map.put("user_id",App.getInstance().getUserId());
        request(repo.requestSelCar(map));
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof CarEntity){
            carEntity = (CarEntity) baseEntity;
            adapter.setNewInstance(carEntity.data);
            totalPrice();
            dialog.dismiss();
            List<OffLineCarEntity> list = new OffLineCarDaoEntity().selectAll();
            if (!list.isEmpty()&& App.getInstance().isLogin()){
                //合并购物车
                ArrayList<CarEntity.Values> data = new ArrayList<>();
                for (int i = 0;i < list.size();i ++){
                    boolean flag = false;
                    for (int j =0;j<carEntity.data.size();j ++){
                        if (carEntity.data.get(j).car_goods.get(0).goods_id ==
                                list.get(i).goods_id){
                            flag = true;
                        }
                    }
                    if (flag)
                        continue;
                    CarEntity.Values values= new CarEntity.Values();
                    values.car_id = 0;
                    values.user_id = 1;
                    List<CarEntity.CarGoodsValues> car_goods = new ArrayList<>();
                    CarEntity.CarGoodsValues values1 = new CarEntity.CarGoodsValues();
                    values1.shop_color = list.get(i).shop_color;
                    values1.shop_num = list.get(i).shop_num;
                    values1.shop_name = list.get(i).shop_name;
                    values1.shop_price = list.get(i).shop_price;
                    values1.shop_select = list.get(i).shop_select;
                    values1.shop_size = list.get(i).shop_size;
                    values1.car_goods_id = list.get(i).getId();
                    values1.car_id = 0;
                    values1.goods_id = list.get(i).goods_id;
                    values1.car_path = list.get(i).car_path;
                    car_goods.add(values1);
                    values.car_goods = car_goods;
                    data.add(values);
                }
                request(repo.requestAddCares(data));
            }
        }else if (baseEntity instanceof SuccessEntity){
            carlist_order_btn.setText("结算");
            carlist_edt_tv.setText("编辑");
            allFlag = false;
            if (dialog !=null)
                dialog.dismiss();
            HashMap<String,Object>map = new HashMap<>();
            map.put("user_id",1);
            request(repo.requestSelCar(map));

        }else if (baseEntity instanceof MergeSuccessEntity){
            if (dialog !=null)
                dialog.dismiss();
            //离线购物车合并成功，清除本地离线购物车数据
            DaoDBUtils.getInstance().getDaoSession().getOffLineCarEntityDao().deleteAll();
            HashMap<String,Object>map = new HashMap<>();
            map.put("user_id",1);
            request(repo.requestSelCar(map));
        }else if (baseEntity instanceof CreateOrderEntity){
            CreateOrderEntity orderEntity = (CreateOrderEntity) baseEntity;
            Intent intent = new Intent(this, CheckOrderActivity.class);
            intent.putExtra("value",orderEntity);
            startActivity(intent);
        }
    }

    private void totalPrice(){
        if (carEntity == null){
            return;
        }
        totalPrice = 0f;
        allFlag = true;
        for (int i = 0; i < carEntity.data.size();i ++){
            CarEntity.Values values = carEntity.data.get(i);
            if (values.selFlag){
                for (int j = 0;j < values.car_goods.size();j ++){
                    int num = Integer.valueOf(values.car_goods.get(j).shop_num);
                    float price = Float.valueOf(values.car_goods.get(j).shop_price);
                    totalPrice+=(price*num);
                }
            }else {
                allFlag = false;
            }
        }
        car_all_tv.setText("合计:"+totalPrice);
        if (allFlag)
            car_sel_all.setImageResource(R.drawable.checkbox_payment_select);
        else
            car_sel_all.setImageResource(R.drawable.checkbox_payment_normal);
    }

    //全选切换
    private void checkSel(){
        if (carEntity == null){
            return;
        }
        for (int i = 0; i < carEntity.data.size();i ++){
            CarEntity.Values values = carEntity.data.get(i);
            values.selFlag = allFlag;
            carEntity.data.set(i,values);
        }
        adapter.notifyDataSetChanged();
        totalPrice();
    }

    private void del(){
        if (carEntity == null){
            return;
        }
        ArrayList<CarEntity.Values>list = new ArrayList<>();
        for (int i = 0; i < carEntity.data.size();i ++){
            CarEntity.Values values = carEntity.data.get(i);
            if (values.selFlag){
                list.add(values);
            }
        }
        if (list.size()>0){
            request(repo.requestDelCares(list));
        }
    }

    @Override
    public void onClick(View v) {
        if (R.id.carlist_back_icon == v.getId()){
            finish();
        }else if (R.id.carlist_order_btn == v.getId()){
            if (carlist_order_btn.getText().equals("删除")){
                del();
            }else {
                subCar();
            }
        }else if (R.id.car_sel_all == v.getId()){
            if (carEntity == null){
                return;
            }
            allFlag = !allFlag;
            checkSel();
        }else if (R.id.carlist_edt_tv == v.getId()){
            if (carEntity == null){
                return;
            }
            if (carlist_edt_tv.getText().toString().equals("编辑")){
                carlist_edt_tv.setText("保存");
                carlist_order_btn.setText("删除");
                allFlag = false;
                checkSel();
            }else {
                carlist_edt_tv.setText("编辑");
                carlist_order_btn.setText("结算");
            }
        }
    }

    //提交订单
    @CheckLogin
    private void subCar(){
        for (int i = 0;i < carEntity.data.size();i ++){
            if (!carEntity.data.get(i).selFlag)
                continue;
            HashMap<String,Object>map = new HashMap<>();
            map.put("order_id",0);
            map.put("user_id",App.getInstance().getUserId());
            map.put("pay_id",0);
            map.put("delivery_id",0);
            map.put("car_id",carEntity.data.get(i).car_id);
            map.put("order_statues",0);
            map.put("ship_id",0);
            list.add(map);
        }
        request(repo.requestSubCar(list));
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        this.adapter.getItem(position).selFlag=!this.adapter.getItem(position).selFlag;
        this.adapter.notifyItemChanged(position);
        totalPrice();
    }

}
